package com.xing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.Utils.Result;
import com.xing.dao.LiveAttentionDao;
import com.xing.dto.LiveAttentionDTO;
import com.xing.dto.LiveAttentionUserDTO;
import com.xing.entity.LiveAttentionEntity;
import com.xing.service.LiveAttentionService;
import com.xing.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LiveAttentionServiceImpl extends ServiceImpl<LiveAttentionDao, LiveAttentionEntity> implements LiveAttentionService {

    @Autowired
    private LiveAttentionDao liveAttentionDao;

    @Override
    public Result attentionTo(LiveAttentionDTO liveAttentionDTO) {
        // 判断必要信息是否缺失
        if (liveAttentionDTO.getAttentionId() != null && liveAttentionDTO.getAttentionedId() != null) {
            // 转换实体类
            LiveAttentionEntity attentionEntity = BeanUtil.toBean(liveAttentionDTO, LiveAttentionEntity.class);
            // 进行插入操作
            int insert = liveAttentionDao.insert(attentionEntity);
            if (insert < 1) {
                return new Result().err(456, "关注失败");
            }
            return new Result().ok("关注成功");
        } else {
            return new Result().err(406, "所需信息缺失");
        }
    }

    @Override
    public Result cancelAttention(LiveAttentionDTO liveAttentionDTO) {
        // 判断必要信息是否缺失
        if (liveAttentionDTO.getAttentionId() != null && liveAttentionDTO.getAttentionedId() != null) {
            // 封装删除条件
            LambdaQueryWrapper<LiveAttentionEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(LiveAttentionEntity.class)
                    .eq(LiveAttentionEntity::getAttentionId, liveAttentionDTO.getAttentionId())
                    .eq(LiveAttentionEntity::getAttentionedId, liveAttentionDTO.getAttentionedId())
                    .eq(LiveAttentionEntity::getDelFlag, 0);
            // 查询操作数据是否存在
            LiveAttentionEntity liveAttentionEntity = liveAttentionDao.selectOne(lambdaQueryWrapper);
            if (liveAttentionEntity == null) {
                return new Result().err(466, "操作关系不存在");
            }
            liveAttentionEntity.setDelFlag(1);
            int update = liveAttentionDao.updateById(liveAttentionEntity);
            if (update < 1) {
                return new Result().err(467, "取消关注失败");
            }
            return new Result().ok("取消关注成功");
        } else {
            return new Result().err(406, "所需信息缺失");
        }
    }

    @Override
    public List<LiveAttentionUserDTO> getAttentionList() {
        // 获取当前登录用户信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 调用方法进行查询
        return liveAttentionDao.selectAttentionListByUserId(user.getId());
    }

    @Override
    public List<LiveAttentionUserDTO> getAttentionedList() {
        // 获取当前登录的用户信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 调用方法查询
        return liveAttentionDao.selectAttentionedListByUserId(user.getId());
    }

    @Override
    public List<LiveAttentionUserDTO> getAttentionListByUserId(Long userId) {
        // 调用方法进行查询
        return liveAttentionDao.selectAttentionListByUserId(userId);
    }

    @Override
    public List<LiveAttentionUserDTO> getAttentionedListByUserId(Long userId) {
        // 调用方法查询
        return liveAttentionDao.selectAttentionedListByUserId(userId);
    }


}
